package com.example.dell.newstart.type;

import android.content.Intent;
import android.support.annotation.NonNull;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;


import com.example.dell.newstart.R;
import com.example.dell.newstart.base.BaseFragment;
import com.example.dell.newstart.search.CustomSearchView;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import butterknife.Unbinder;

/**
 * created by Dongkai on 2018/12/4
 */

public class TypeFragment extends BaseFragment implements CustomSearchView.OnSearchListener, View.OnClickListener {

    /**
     * 美发馆
     */
    public static final int MFG = 100007;
    /**
     * 护肤馆
     */
    public static final int HFG = 100008;
    /**
     * 彩妆馆
     */
    public static final int CZG = 100009;
    /**
     * 香氛馆
     */
    public static final int XFG = 100010;
    /**
     * 男士馆
     */
    public static final int NSG = 100011;

    /**
     * 电脑
     */
    public static final int PC = 100012;
    /**
     * 手机
     */
    public static final int PHONE = 100013;
    /**
     * 数码相机
     */
    public static final int SMXJ = 100014;
    /**
     * 3C配件
     */
    public static final int PJ = 100015;
    /**
     * 3冰箱
     */
    public static final int JDBX = 100016;

    /**
     * 电视
     */
    public static final int JDDS = 100017;
    /**
     * 洗衣机
     */
    public static final int JDXYJ = 100018;
    /**
     * 空调
     */
    public static final int JDKT = 100019;
    /**
     * 热水器
     */
    public static final int JDRSQ = 100020;
    /**
     * 水果
     */
    public static final int SG = 100021;

    /**
     * 海鲜
     */
    public static final int HX = 100022;
    /**
     * 禽蛋肉类
     */
    public static final int QDRL = 100023;
    /**
     * 帆布鞋
     */
    public static final int XXFBX = 100024;
    /**
     * 皮鞋
     */
    public static final int XXPX = 100025;
    /**
     * 凉拖
     */
    public static final int XXLT = 100026;

    /*
     * 运动鞋
     */
    public static final int XXYDX = 100024;
    /**
     * 高跟鞋
     */
    public static final int XXGGX = 100028;
    /**
     * 当前选中的类别
     */
    public static int CURRENT_TYPE = 100006;
    @BindView(R.id.ll_type_one)
    LinearLayout llTypeOne;
    @BindView(R.id.ll_type_two)
    LinearLayout llTypeTwo;
    @BindView(R.id.ll_type_three)
    LinearLayout llTypeThree;
    @BindView(R.id.ll_type_four)
    LinearLayout llTypeFour;
    @BindView(R.id.ll_type_five)
    LinearLayout llTypeFive;
    @BindView(R.id.ll_type_six)
    LinearLayout llTypeSix;
    @BindView(R.id.iv_good)
    ImageView goodImageView;
    @BindView(R.id.iv_five)
    ImageView fiveImageView;
    @BindView(R.id.iv_six)
    ImageView sixImageView;
    @BindView(R.id.iv_seven)
    ImageView sevenImageView;
    @BindView(R.id.iv_eight)
    ImageView eightImageView;
    @BindView(R.id.iv_nine)
    ImageView nineImageView;
    Unbinder unbinder;
    @BindView(R.id.vp_type)
    ViewPager viewPager;

    private CustomSearchView customSearchView;

    private List<View> viewList = new ArrayList<>();//各大类别视图的集合
    private LayoutInflater inflater;
    private LinearLayout one, two, three, four, five, six;


    @Override
    protected int initLayout() {
        return R.layout.fragment_type_3;
    }

    @Override
    protected void initView() {
        unbinder = ButterKnife.bind(this, getContentView());

        inflater = getLayoutInflater();

        View typeOne = inflater.inflate(R.layout.detail_type_one, null);
        View typeTwo = inflater.inflate(R.layout.detail_type_two, null);
        View typeThree = inflater.inflate(R.layout.detail_type_three, null);
        View typefour = inflater.inflate(R.layout.detail_type_four, null);
        View typefive = inflater.inflate(R.layout.detail_type_five, null);
        View typeSix = inflater.inflate(R.layout.detail_type_six, null);

        one = typeOne.findViewById(R.id.ll_mfg);
        one.setOnClickListener(this);

        viewList.add(typeOne);
        viewList.add(typeTwo);
        viewList.add(typeThree);
        viewList.add(typefour);
        viewList.add(typefive);
        viewList.add(typeSix);

       customSearchView = getContentView().findViewById(R.id.csv_type);
        //设置搜索监听
        customSearchView.setOnSearchListener(this);

    }

    @Override
    protected void initData() {
        viewPager.setAdapter(new PagerAdapter() {
            @Override
            public int getCount() {
                return viewList.size();
            }

            @Override
            public boolean isViewFromObject(@NonNull View view, @NonNull Object o) {
                return view == o;
            }

            /**
             * 实例化一个页卡
             */
            @Override
            public Object instantiateItem(ViewGroup container, int position) {
                container.addView(viewList.get(position));
                return viewList.get(position);
            }

            /**
             * 销毁一个页卡
             */
            @Override
            public void destroyItem(ViewGroup container, int position, Object object) {

                container.removeView(viewList.get(position));

            }

        });
        onTypeSelected(0);
    }


    /**
     * 把产品类别id传到{@link ProductListActivity}活动
     *//*
        intent.putExtra("categoryID", CURRENT_TYPE);
        startActivity(intent);
    }*/
    @Override
    public void onDestroyView() {
        super.onDestroyView();
        unbinder.unbind();
    }

    @OnClick({R.id.ll_type_one, R.id.ll_type_two, R.id.ll_type_three, R.id.ll_type_four, R.id.ll_type_five, R.id.ll_type_six})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.ll_type_one:
                onTypeSelected(0);
                break;
            case R.id.ll_type_two:
                onTypeSelected(1);
                break;
            case R.id.ll_type_three:
                onTypeSelected(2);
                break;
            case R.id.ll_type_four:
                onTypeSelected(3);
                break;
            case R.id.ll_type_five:
                onTypeSelected(4);
                break;
            case R.id.ll_type_six:
                onTypeSelected(5);
                break;
        }
    }


    /**
     * 选中某一大项执行的逻辑
     *
     * @param i 选中的大项
     */
    private void onTypeSelected(int i) {
        resetAllTypesSelected();
        resetAllImageView();
        viewPager.setCurrentItem(i);//让右边布局随着左边点击切换
        switch (i) {
            case 0:
                llTypeOne.setSelected(true);
                goodImageView.setImageResource(R.mipmap.wqq_checked);
                break;
            case 1:
                llTypeTwo.setSelected(true);
                fiveImageView.setImageResource(R.mipmap.zlt_checked);
                break;
            case 2:
                llTypeThree.setSelected(true);
                sixImageView.setImageResource(R.mipmap.lx_checked);
                break;
            case 3:
                llTypeFour.setSelected(true);
                sevenImageView.setImageResource(R.mipmap.hch_checked);
                break;
            case 4:
                llTypeFive.setSelected(true);
                eightImageView.setImageResource(R.mipmap.hyc_checked);
                break;
            case 5:
                llTypeSix.setSelected(true);
                nineImageView.setImageResource(R.mipmap.zs_checked);
                break;
        }
    }



    //将所有ImageView的图片设为灰色
    private void resetAllImageView() {
        goodImageView.setImageResource(R.mipmap.wqq_nomal);
        fiveImageView.setImageResource(R.mipmap.zlt_nomal);
        sixImageView.setImageResource(R.mipmap.lx_nomal);
        sevenImageView.setImageResource(R.mipmap.hch_nomal);
        eightImageView.setImageResource(R.mipmap.hyc_nomal);
        nineImageView.setImageResource(R.mipmap.zs_nomal);
    }
    private void showTypeDetail(int i) {

    }

    /**
     * 把所有大类重置为没有选中状态
     */
    private void resetAllTypesSelected() {
        llTypeOne.setSelected(false);
        llTypeTwo.setSelected(false);
        llTypeThree.setSelected(false);
        llTypeFour.setSelected(false);
        llTypeFive.setSelected(false);
        llTypeSix.setSelected(false);
    }


    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.ll_mfg:
                String toastString = one.getTag().toString();
                toastShort(toastString);
                CURRENT_TYPE = MFG;
                break;
            case R.id.ll_hfg:
                CURRENT_TYPE = HFG;
                break;
            case R.id.ll_czg:
                CURRENT_TYPE = CZG;
                break;
            case R.id.ll_xfg:
                CURRENT_TYPE = XFG;
                break;
            case R.id.ll_nsg:
                CURRENT_TYPE = NSG;
                break;
            case R.id.ll_pc:
                CURRENT_TYPE = PC;
                break;
            case R.id.ll_phone:
                CURRENT_TYPE = PHONE;
                break;
            case R.id.ll_smxj:
                CURRENT_TYPE = SMXJ;
                break;
            case R.id.ll_pj:
                CURRENT_TYPE = PJ;
                break;
            case R.id.ll_jdbx:
                CURRENT_TYPE = JDBX;
                break;
            case R.id.ll_jdds:
                CURRENT_TYPE = JDDS;
                break;
            case R.id.ll_jdxyj:
                CURRENT_TYPE = JDXYJ;
                break;
            case R.id.ll_jdkt:
                CURRENT_TYPE = JDKT;
                break;
            case R.id.ll_jdrsq:
                CURRENT_TYPE = JDRSQ;
                break;
            case R.id.ll_sg:
                CURRENT_TYPE = SG;
                break;
            case R.id.ll_hx:
                CURRENT_TYPE = HX;
                break;
            case R.id.ll_qdrl:
                CURRENT_TYPE = QDRL;
                break;
            case R.id.ll_xxfbx:
                CURRENT_TYPE = XXFBX;
                break;
            case R.id.ll_xxpx:
                CURRENT_TYPE = XXPX;
                break;
            case R.id.ll_xxlt:
                CURRENT_TYPE = XXLT;
                break;
            case R.id.ll_xxydx:
                CURRENT_TYPE = XXYDX;
                break;
            case R.id.ll_xxggx:
                CURRENT_TYPE = XXGGX;
                break;

        }
        //启动我们展示产品列表的活动
        Intent intent = new Intent(getContext(), ProductListActivity.class);
        /**
         * 把产品类别id传到{@link ProductListActivity}活动
         */
       /* intent.putExtra("categoryID", CURRENT_TYPE);
        startActivity(intent);*/
    }

    //点击搜索按钮执行的方法
    @Override
    public void onSearch(String keywords) {
       /* toastShort("调用搜索接口，根据关键词：" + keywords + "搜索商品");
        //启动搜索 和 显示搜索结果 的活动
        Intent intent = new Intent(getContext(), ProductListActivity.class);
        intent.putExtra("keywords", keywords);
        startActivity(intent);*/
    }
}

